package com.ljb.lib_hiknvr.nvr.play;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.ljb.lib_hiknvr.nvr.ThreadUtils;
import com.ljb.lib_hiknvr.nvr.controller.Callback;
import com.ljb.lib_hiknvr.nvr.controller.HikNVRController;


/**
 * 视频播放的view控件,
 * Created by ljb on 2018/8/13.
 */
public class NVRPlayView extends FrameLayout implements View.OnTouchListener {
    public interface OnTapClickListener {

        /**
         * 单击
         */
        void onSingleTap();

        /**
         * 双击
         */
        void onDoubleTap();

    }

    private static final String TAG = "PlayView";
    //预览播放控件
    private PlaySurfaceView mPlaySurfaceView;
    //点击监听器
    private OnTapClickListener mOnTapClickListener;

    private int mUserID = -1;
    private int mChannel = -1;
    private int mStreamType = 1;


    public NVRPlayView(@NonNull Context context) {
        this(context, null);
    }

    public NVRPlayView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NVRPlayView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnTouchListener(this);
        setClickable(true);
        setFocusable(true);
        setLongClickable(true);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }


    //手势识别器
    private GestureDetector mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
        //单击
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            if (mOnTapClickListener != null)
                mOnTapClickListener.onSingleTap();
            return super.onSingleTapUp(e);
        }

        //双击
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            if (mOnTapClickListener != null)
                mOnTapClickListener.onDoubleTap();
            return super.onDoubleTap(e);
        }
    });


    //++++++++++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++++++++


    /**
     * 获取预览句柄
     *
     * @return
     */
    public int getRealHandle() {
        return mPlaySurfaceView != null ? mPlaySurfaceView.getRealHandle() : -1;
    }

    /**
     * 获取PlaySurfaceView
     *
     * @return
     */
    public PlaySurfaceView getmPlaySurfaceView() {
        return mPlaySurfaceView;
    }

    /**
     * 开始预览
     *
     * @param userID     用户id
     * @param channel    通道号
     * @param streamType 码流类型
     */
    public void startPlay(int userID, int channel, int streamType,OnPlayStateListener listener) {
        mUserID = userID;
        mChannel = channel;
        mStreamType = streamType;

        PlaySurfaceView.PlaySurfaceViewBuilder builder = new PlaySurfaceView.PlaySurfaceViewBuilder(getContext())
                .setUserID(mUserID)
                .setChannel(mChannel)
                .setStreamType(mStreamType);

        if (mPlaySurfaceView == null) {
            mPlaySurfaceView = builder.create();
            addView(mPlaySurfaceView);
        } else {
            mPlaySurfaceView.setBuilder(builder);
        }

        mPlaySurfaceView.setOnPlayStateListener(listener);

        if (mPlaySurfaceView.getVisibility() != View.VISIBLE)
            mPlaySurfaceView.setVisibility(View.VISIBLE);
    }

    /**
     * 停止预览
     */
    public void stopPlay(OnPlayStateListener listener) {
        if (mPlaySurfaceView != null) {
            mPlaySurfaceView.setOnPlayStateListener(listener);

            if (mPlaySurfaceView.getVisibility() != View.GONE)
                mPlaySurfaceView.setVisibility(View.GONE);
        }
    }

    /**
     * 云台控制
     *
     * @param ptzCommand 云台控制命令,{@link com.ljb.lib_hiknvr.nvr.controller.PTZCommand}
     * @param callback   回调
     */
    public void ptzControl(final int ptzCommand, final Callback<Boolean> callback) {
        ThreadUtils.runInThread(new Runnable() {
            @Override
            public void run() {
                boolean ret = HikNVRController.getInstance().ptzControl(mUserID, mChannel, ptzCommand);
                if (callback != null)
                    callback.callback(ret);
            }
        });
    }

    /**
     * 设备抓图
     *
     * @param path     保存路径
     * @param callback 回调
     * @return 设备抓图结果
     */
    public void capture(final String path, final Callback<Boolean> callback) {
        ThreadUtils.runInThread(new Runnable() {
            @Override
            public void run() {
                boolean ret = HikNVRController.getInstance().capture(mUserID, mChannel, path);
                if (callback != null)
                    callback.callback(ret);
            }
        });
    }


    /**
     * 开始捕获预览视频
     *
     * @param path
     * @param callback
     */
    public void startRecord(final String path, final Callback<Boolean> callback) {
        ThreadUtils.runInThread(new Runnable() {
            @Override
            public void run() {
                if (mPlaySurfaceView != null) {
                    boolean ret = HikNVRController.getInstance().startRecord(mPlaySurfaceView.getRealHandle(), path);
                    if (callback != null)
                        callback.callback(ret);
                }
            }
        });
    }

    /**
     * 停止捕获预览视频
     *
     * @param callback
     */
    public void stopRecord(final Callback<Boolean> callback) {
        ThreadUtils.runInThread(new Runnable() {
            @Override
            public void run() {
                if (mPlaySurfaceView != null) {
                    boolean ret = HikNVRController.getInstance().stopRecord(mPlaySurfaceView.getRealHandle());
                    if (callback != null)
                        callback.callback(ret);
                }
            }
        });
    }

    /**
     * 设置点击监听器
     *
     * @param onTapClickListener
     */
    public void setOnTapClickListener(OnTapClickListener onTapClickListener) {
        mOnTapClickListener = onTapClickListener;
    }
}
